<template>
  <el-dialog
    :title="headTitle"
    :visible.sync="dialogVisible"
    width="900px"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <div class="dialog-content">
      <p>{{ headTitle }}前</p>
      <div class="table-1">
        <el-table
          :data="listData1"
          v-if="headTitle === '修改交易记录' || headTitle === '删除交易记录'"
        >
          <el-table-column
            label="日期"
            align="center"
            prop="accountTime"
            :formatter="filtime"
          />
          <el-table-column label="项目" align="center" prop="deptName" />
          <el-table-column
            label="资金类型"
            align="center"
            prop="accountFundTypeName"
          />
          <el-table-column label="账户" align="center" prop="fundAccountName" />
          <el-table-column label="收支" align="center" prop="accountType">
            <template #default="scope">
              {{ scope.row.accountType === 1 ? "收入" : "支出" }}
            </template>
          </el-table-column>
          <el-table-column label="金额" align="center" prop="accountAmount" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="填写人" align="center" prop="createBy" />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="filtime"
          />
          <el-table-column
            label="提交时间"
            align="center"
            prop="updateTime"
            :formatter="filtime"
          />
        </el-table>
        <el-table :data="listData1" v-else>
          <el-table-column label="账户名" align="center" prop="name" />
          <el-table-column label="金额" align="center" prop="accountAmount" />
          <el-table-column label="填写人" align="center" prop="createBy" />
          <el-table-column
            label="提交时间"
            align="center"
            prop="createTime"
            :formatter="filtime"
          />
        </el-table>
      </div>
      <p v-show="isShow">{{ headTitle }}后</p>
      <div class="table-2" v-show="isShow">
        <el-table
          :data="listData2"
          v-if="headTitle === '修改交易记录' || headTitle === '删除交易记录'"
        >
          <el-table-column
            label="日期"
            align="center"
            prop="accountTime"
            :formatter="filtime"
          />
          <el-table-column label="项目" align="center" prop="deptName" />
          <el-table-column
            label="资金类型"
            align="center"
            prop="accountFundTypeName"
          />
          <el-table-column label="账户" align="center" prop="fundAccountName" />
          <el-table-column label="收支" align="center" prop="accountType">
            <template #default="scope">
              {{ scope.row.accountType === 1 ? "收入" : "支出" }}
            </template>
          </el-table-column>
          <el-table-column label="金额" align="center" prop="accountAmount" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="填写人" align="center" prop="createBy" />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="filtime"
          />
          <el-table-column
            label="提交时间"
            align="center"
            prop="updateTime"
            :formatter="filtime"
          />
        </el-table>
        <el-table :data="listData2" v-else>
          <el-table-column label="账户名" align="center" prop="name" />
          <el-table-column label="金额" align="center" prop="accountAmount" />
          <el-table-column label="填写人" align="center" prop="createBy" />
          <el-table-column
            label="提交时间"
            align="center"
            prop="createTime"
            :formatter="filtime"
          />
        </el-table>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    headTitle: {}
  },
  data() {
    return {
      dialogVisible: false,
      listData1: [],
      listData2: [],
      isShow: false
    };
  },
  methods: {
    open(data) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.headTitle === "修改交易记录") {
          this.isShow = true;
          this.listData1 = JSON.parse(data.jsonResult).data
            ? [JSON.parse(data.jsonResult).data]
            : [];
          this.listData2 = data.operParam ? [JSON.parse(data.operParam)] : [];
        }
        if (this.headTitle === "删除交易记录") {
          this.isShow = false;
          this.listData1 = JSON.parse(data.jsonResult).data
            ? [JSON.parse(data.jsonResult).data]
            : [];
        }
        if (this.headTitle === "修改账户") {
          this.isShow = true;
          this.listData1 = JSON.parse(data.jsonResult).data
            ? [JSON.parse(data.jsonResult).data]
            : [];
          this.listData2 = data.operParam ? [JSON.parse(data.operParam)] : [];
        }
        if (this.headTitle === "删除账户") {
          this.isShow = false;
          this.listData1 = JSON.parse(data.jsonResult).data
            ? [JSON.parse(data.jsonResult).data]
            : [];
        }
      });
      console.log(JSON.parse(data.jsonResult).data);
    },
    updateKey() {
      this.$emit("update:keyIndex", new Date().getTime());
    },
    handleClose(done) {
      this.dialogVisible = false;
      this.updateKey();
    },
    filtime(row, column, cellValue, index) {
      if (!cellValue) {
        return "--";
      }
      let timestamp4 = new Date(cellValue); //直接用 new Date(时间戳) 格式转化获得当前时间
      // let time=timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8); //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式
      let time = timestamp4.toLocaleDateString().replace(/\//g, "-"); //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式
      return time;
    }
  }
};
</script>

<style lang="scss" scoped>
.el-dialog__wrapper {
  /deep/ .el-dialog {
    height: 400px;

    .table-2 {
      .el-table__header {
        display: none;
      }
    }
  }
}
</style>
